package com.sxhuayuan.parking.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class AlipayUtils {

	/**
	 * 由于设置获取用户信息的回调域名限制为1个，所以无法同时在两个域名获取auth_code，解决办法是在pay.sxhytech. com服务器放一个中间页面
	 * (pay.sxhytech.com/alipay/get-code.html)，pay以外的其他域名只须请求get-code页面并加上回调
	 * ，它会发起授权，然后转发code和state参数到回调地址,<br>
	 * 参考https://github.com/HADB/GetWeixinCode
	 * 
	 * @author gaoyan
	 * @return
	 * @since JDK 1.7
	 */
	public static String connectOauth2Authorize(String appid, String redirectUri, boolean userInfo, String state) {
		String url = "http://pay.sxhytech.com/alipay/get-code.html?app_id=%s&scope=%s&state=%s&redirect_uri=%s";
		try {
			return String.format(url, appid, (userInfo ? "auth_user" : "auth_base"), (state == null ? "" : state), URLEncoder.encode(redirectUri, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
}
